package com.git.groovy.groovyScript

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import groovy.json.JsonBuilder
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.junit.jupiter.api.Test

class Test3 {

    @Test
    public void test1(){
        println("ttt");
    }

    @Test
    public void testJson(){
        //xx
        def jsonStr = '{"aa":"bb","cc":"dd","name":"nameA","ID":"idA","Jdd":{"bc":"bcTest"}}';
        JSONObject jsonObject = JSON.parse(jsonStr);
        println jsonObject.getString("aa")

        def jsonSlurper = new JsonSlurper()
        def jsonObj = jsonSlurper.parseText( jsonStr )

        println jsonObj.name
        println jsonObj.name
        println jsonObj.ID
        jsonObj.ID = "IDAA"

        println jsonObj.ID
        println jsonObj
        println(JsonOutput.toJson(jsonObj))

        println jsonObj.Jdd.bc;
        println jsonObj.Jdd.bcff;
        println jsonObj.aaaff.aa;//java.lang.NullPointerException: Cannot get property 'aa' on null object
    }

    @Test
    public void testGroovyJson(){
        def tt = ["test", "test1"]
        def jjj = "jason"
        def js3 = new groovy.json.JsonBuilder()
        def js2 = new groovy.json.JsonBuilder(tt);
        js3 hello: "$jjj", "$jjj": tt
        def js4 = new groovy.json.JsonBuilder()
        def result = js4([sdn: js3, openflow: js2, type: 3])
        println js4.toPrettyString()

        def js5 = new JsonBuilder('{"aa":"bb","cc":"add"}')
        def js6 = new JsonBuilder(js5.content)
        println js6.toString() //"{\"aa\":\"bb\",\"cc\":\"add\"}"
        println js5.content // {"aa":"bb","cc":"add"}
    }


    public static void main(String[] args) {
        println("ttt");
    }
}
